<?php


namespace app\admin\controller;
use app\admin\model\Announcement;
use app\admin\model\Item;
use app\BaseController;
use think\facade\View;
use think\Request;

class System extends BaseController
{

    //显示资源
	public function index(Request $request)
	{
	    if ($request->isAjax()){
            //获取每页显示的条数
            $limit = $request->param('limit');
            //获取当前页数
            $page = $request->param('page');
            $result = Announcement::paginate($limit, false, ['page' => $page])->toArray();
            return json(['code' => 200, 'msg' => '', 'total' => $result['total'], 'data' => $result['data']]);
        }
		return View::fetch();
	}
    //新增
	public function add(Request $request){
	    if ($request->isAjax()){
	        $data = $request->param();
	        unset($data['test-editormd-html-code']);
	        $data['admin_id'] = $this->auth->id;
	        $res = Announcement::create($data);
	        if ($res){
                return json(['code' => 200, 'url' => '', 'msg' => '添加成功！']);
            }
        }
	    return View::fetch();
    }
    //编辑
    public function edit(Request $request,$id){
        $announcement = Announcement::find($id);
        if ($request->isAjax()){
            $data = $request->param();
            unset($data['test-editormd-html-code']);
            $data['admin_id'] = $this->auth->id;
            $res = Announcement::update($data);
            if ($res){
                return json(['code' => 200, 'url' => '', 'msg' => '编辑成功！']);
            }
        }
        View::assign('model',$announcement);
        return View::fetch();
    }

    public function del($ids = ""){
        if ($ids) {
            $ids = explode(',', $ids);
            $count = Announcement::destroy(array_values($ids));
            if ($count) {
                return json(['code' => 200, 'url' => '', 'msg' => '删除成功！']);
            }
        }
    }
}